﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;


///========================================================================
/// Summary	: <Base Form for almost form inherit>
/// Author	: <Van Vo>, <20070518>
/// 
/// Revision History:
/// 
/// [20070518]		[Van Vo]		[Create]
/// [20070530]      [Vao Vo]        [Remove Form Title]
///========================================================================			
		


namespace Lapas.Common.Forms
{
    public class BaseForm : Form
    {
        #region Loading handler
        public delegate void UpdateUI_Delegate(bool IsDataLoaded, string title);
        public delegate void UpdateCursorUI_Delegate(Form form, bool isWaitCursor);
        public delegate void ShowReport_Delegate(System.Windows.Forms.IWin32Window owner, CrystalDecisions.CrystalReports.Engine.ReportClass reportDocumment);
        public delegate void PrintReportDocument_Delegate(CrystalDecisions.CrystalReports.Engine.ReportClass reportDocumment);

        protected void UpdateUI(bool IsDataLoaded, string title)
        {
            if (IsDataLoaded)
            {
                Helper.Loading_Form_End(title);
            }
            else
            {
                Helper.Loading_Form_Begin(title);
            }
        }       
        //protected void ShowReport(System.Windows.Forms.IWin32Window owner, CrystalDecisions.CrystalReports.Engine.ReportClass reportDocumment)
        //{
        //    Globals.ShowReport(owner, reportDocumment);
        //}

        protected virtual void BeginLoad(string title)
        {
           
            Invoke(new UpdateCursorUI_Delegate(Helper.UpdateCursorUI), new object[] { this, true });
            Invoke(new UpdateUI_Delegate(UpdateUI), new object[] { false, title });
        }
        protected virtual void EndLoad(string title)
        {
            System.Threading.Thread.Sleep(300);
            Invoke(new UpdateCursorUI_Delegate(Helper.UpdateCursorUI), new object[] { this, false });
            Invoke(new UpdateUI_Delegate(UpdateUI), new object[] { true, title });
        }
       
        #endregion

        //public BaseForm() : base() { }
        /// <summary>
        /// Sets default values to form
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
          
            // For Controlling Key down
            this.KeyPreview = true;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MinimizeBox = false;
            //this.Icon = Icons.formIcon;
            if (allowBindDefaultForm)
            {
                BindDefaultForm();
            }           
        }

        /// <summary>
        /// Handle Key Down
        /// F10: Exit
        /// Esc: Default Value
        /// Enter: Go to next control
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
       
            
            if (e.KeyCode == Keys.Enter)
            {
                // Neu bo tac dung phim Enter thi thoat
                if (_disableEnterButton) return;

                SelectNextControl(this.ActiveControl, true, true, true,true);
                if (this.ActiveControl.GetType() == typeof(TextBox))
                {
                    this.ActiveControl.Focus();
                    ((TextBox)this.ActiveControl).SelectAll();
                }
                
                               
            }
            if (e.KeyCode == Keys.Escape)
            {
                if (!allowBindDefaultForm) return;
                BindDefaultForm();
            }
            if (e.KeyCode == Keys.F10)
            {
                e.Handled = true;
                Close();
            }
            
        }

        public virtual void BindDefaultForm()
        {
            if (ValidateLicense())
            {
                foreach (Control control in Controls)
                {
                    // Check cho truong hop control ke thua tu TextBox
                    Control controlTemp = control as TextBox;
                    if (control != null)
                    {
                        Control childControl = control.GetNextControl(control, true);
                        while (childControl != null)
                        {
                            // Check cho truong hop control ke thua tu TextBox
                            Control childControlTemp = childControl as TextBox;
                            if (childControlTemp != null)
                            {
                                ((TextBox)childControl).Text = string.Empty;
                                if (childControl.TabIndex == 0 && childControl.TabStop)
                                {
                                    childControl.Focus();
                                    ((TextBox)childControl).SelectAll();
                                }
                                childControl = control.GetNextControl(childControl, true);
                                continue;

                            }
                            if (childControl.GetType() == typeof(TextBox))
                            {
                                ((TextBox)childControl).Text = string.Empty;
                            }
                            else if (childControl.GetType() == typeof(MaskedTextBox))
                            {
                                ((MaskedTextBox)childControl).Text = string.Empty;
                            }

                            else if (childControl.GetType() == typeof(ComboBox))
                            {
                                //Bỏ để giá trị mặc định
                                //((ComboBox)childControl).SelectedIndex = -1;
                            }
                            else
                            {
                                childControl = control.GetNextControl(childControl, true);
                                continue;
                            }

                            if (childControl.TabIndex == 0 && childControl.TabStop)
                            {
                                if (childControl.GetType() == typeof(TextBox))
                                {
                                    childControl.Focus();
                                    ((TextBox)childControl).SelectAll();
                                }
                                else
                                {
                                    childControl.Focus();
                                    childControl.Select();
                                }
                            }

                            childControl = control.GetNextControl(childControl, true);
                        }
                        if (controlTemp != null) ((TextBox)control).Text = string.Empty;
                        if (control.GetType() == typeof(TextBox)) ((TextBox)control).Text = string.Empty;
                        //if (control.GetType() == typeof(ComboBox)) ((ComboBox)control).SelectedIndex = -1;
                        if (control.GetType() == typeof(MaskedTextBox)) ((MaskedTextBox)control).Text = string.Empty;

                        if (control.TabIndex == 0 && control.TabStop)
                        {
                            if (control.GetType() == typeof(TextBox))
                            {
                                control.Focus();
                                ((TextBox)control).SelectAll();
                            }
                            else
                            {
                                control.Focus();
                                control.Select();
                            }
                        }
                    }

                }
            }
            else
            {
                foreach (Control control in Controls)
                {
                    control.Enabled = false;
                }
            }
        }

        /// <summary>
        /// Dung cho form chi xem khong duoc edit
        /// 20071026 TriDM: bo qua truong hop TabControl
        /// </summary>
        public virtual void BindReadOnlyForm()
        {
            if (ValidateLicense())
            {
                foreach (Control control in Controls)
                {
                    //// Check cho truong hop control ke thua tu TextBox

                    if (control != null)
                    {
                        Control childControl = control.GetNextControl(control, true);
                        while (childControl != null)
                        {
                            if (childControl.GetType() == typeof(GroupBox) || childControl.GetType() == typeof(Button)
                                || childControl.GetType() == typeof(Label) || childControl.GetType() == typeof(LinkLabel)
                                || childControl.GetType() == typeof(TabPage))
                            {
                                childControl = control.GetNextControl(childControl, true);
                                continue;
                            }

                            if (childControl.GetType() == typeof(TextBox))
                            {
                                ((TextBox)childControl).ReadOnly = true;
                            }
                            else if (childControl.GetType() == typeof(MaskedTextBox))
                            {
                                ((MaskedTextBox)childControl).ReadOnly = true;
                            }
                            else if (childControl.GetType() == typeof(DataGridView))
                            {
                                // 20080122 TriDM: bo ReadOnly o GridView
                                //((DataGridView)childControl).ReadOnly = false;
                            }
                            else
                            {
                                Control childControlTemp = childControl as TextBox;
                                if (childControlTemp != null)
                                {
                                    ((TextBox)childControl).ReadOnly = true;
                                }
                                else
                                {

                                    childControl.Enabled = false;
                                }

                            }

                            childControl = control.GetNextControl(childControl, true);
                        }

                        if (control.GetType() == typeof(GroupBox) || control.GetType() == typeof(Button)
                                 || control.GetType() == typeof(Label) || control.GetType() == typeof(LinkLabel))
                        {

                            continue;
                        }

                        if (control.GetType() == typeof(TextBox))
                        {
                            ((TextBox)control).ReadOnly = true;
                        }
                        else if (control.GetType() == typeof(MaskedTextBox))
                        {
                            ((MaskedTextBox)control).ReadOnly = true;
                        }
                        // 20071026 TriDM
                        else if (control.GetType() != typeof(TabControl))
                        {
                            Control childControlTemp = control as TextBox;
                            if (childControlTemp != null)
                            {
                                ((TextBox)control).ReadOnly = true;
                            }
                            else
                            {

                                control.Enabled = false;
                            }

                        }


                    }

                }
            }
            else
            {
                foreach (Control control in Controls)
                {
                    control.Enabled = false;
                }
            }
        }


        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseForm));
            this.SuspendLayout();
            // 
            // BaseForm
            // 
            this.ClientSize = new System.Drawing.Size(282, 255);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "BaseForm";
            this.ResumeLayout(false);

        }

        

        /// <summary>
        /// Cho phép chạy hàm BindDefault lúc khởi tạo hay không ?
        /// </summary>
        private bool allowBindDefaultForm=true;

        public bool AllowBindDefaultForm
        {
            get { return allowBindDefaultForm; }
            set { allowBindDefaultForm = value; }
        }

        private bool _disableEnterButton;
        /// <summary>
        /// Thiet lap phim Enter co tac dung: true
        /// </summary>
        public bool DisableEnterButton
        {
            get { return _disableEnterButton; }
            set { _disableEnterButton = value; }
        }

        private ErrorProvider _BaseFormErrorProvider = new ErrorProvider();

        protected ErrorProvider BaseFormErrorProvider
        {
            get { return _BaseFormErrorProvider; }
            set { _BaseFormErrorProvider = value; }
        }

        protected virtual void RaiseError(Control control, string message)
        {
            RaiseError(control, message, false);
        }
        protected virtual void RaiseError(Control control, string message, bool showDialog)
        {
            if (showDialog) DisplayMessage.ShowError(message);
            BaseFormErrorProvider.SetError(control, message);
        }

        #region License data
        /// <summary>
        /// Hàm này sẽ dùng để kiểm tra form có license hợp lệ hay không? Mặc định luôn là hợp lệ
        /// </summary>
        /// <returns></returns>
        protected virtual bool ValidateLicense()
        {
            return true;
        }
        #endregion

        #region Trace
        /// <summary>
        /// Ham Trace với message
        /// </summary>
        /// <param name="message">message - nội dung</param>
        public virtual void Trace(string message)
        {
            AppSettings.CurrentTrace.RaiseTraceMessage(this, message);
        }
        #endregion

    }
}
